package com.lorenzogao.pley.ui.fragment

import android.annotation.SuppressLint
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.lorenzogao.pley.R
import com.lorenzogao.pley.bean.AppInfo
import com.lorenzogao.pley.common.Constant
import com.lorenzogao.pley.common.util.DateUtils
import com.lorenzogao.pley.ui.adapter.AppInfoAdapter
import com.lorenzogao.pley.ui.base.BaseFragment
import com.lorenzogao.pley.ui.di.component.AppComponent
import com.lorenzogao.pley.ui.di.component.DaggerAppDetailComponent
import com.lorenzogao.pley.ui.di.moduel.AppDetailModule
import com.lorenzogao.pley.ui.di.moduel.AppModuelModule
import com.lorenzogao.pley.ui.presenter.AppDetailPresenter
import com.lorenzogao.pley.ui.presenter.contract.AppInfoContract
import kotlinx.android.synthetic.main.fragment_app_detail.*
import java.util.*


/**
 * 作者：Lorenzo Gao
 * Date: 2018/1/9
 * Time: 16:36
 * 邮箱：2508719070@qq.com
 * Description:
 */
@SuppressLint("ValidFragment")
class AppDetailFragment @SuppressLint("ValidFragment") constructor
(id: Int) : BaseFragment<AppDetailPresenter>(), AppInfoContract.AppDetailView {


    private var mId = id

    override fun showResult(appinfo: AppInfo) {
        showScreenshot(appinfo.screenshot)
        view_introduction.text = appinfo.introduction
        txt_update_time.text = DateUtils.formatDate(appinfo.updateTime)
        txt_apk_size.text = (appinfo.apkSize / 1024 / 1024).toString() + "MB"
        txt_version.text = appinfo.versionName
        txt_publisher.text = appinfo.publisherName
        txt_publisher2.text = appinfo.publisherName


        var mAdapter = AppInfoAdapter.builder().layout(R.layout.template_appinfo2).build()

        recycler_view_same_dev.layoutManager =LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)
        mAdapter.addData(appinfo.sameDevAppInfoList)
        recycler_view_same_dev.adapter = mAdapter


        mAdapter = AppInfoAdapter.builder().layout(R.layout.template_appinfo2).build()

        recycler_view_relate.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)
        mAdapter.addData(appinfo.relateAppInfoList)
        recycler_view_relate.adapter = mAdapter

    }

    override fun setLayout(): Int {
        return R.layout.fragment_app_detail
    }

    override fun initView() {
        mPresenter.getAppDetail(mId)
    }


    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerAppDetailComponent.builder().appComponent(appComponent)
                .appDetailModule(AppDetailModule(this))
                .appModuelModule(AppModuelModule())
                .build().injectFragment(this)
    }


    private fun showScreenshot(screentShot: String) {


        val urls = Arrays.asList(*screentShot.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray())


        for (url in urls) {

            val imageView = LayoutInflater.from(activity).inflate(R.layout.template_imageview, view_gallery, false) as ImageView
            imageView.adjustViewBounds = true
            imageView.scaleType = ImageView.ScaleType.FIT_XY

            val layoutParams = ViewGroup.LayoutParams(480, 640)
            imageView.layoutParams = layoutParams

           Glide.with(this).load(Constant.BASEURL + url).into(imageView)

            view_gallery.addView(imageView)

        }


    }


}